package com.brisk.web.controller.applet;

import com.ruoyi.applet.domain.AppletSelect;
import com.ruoyi.applet.domain.AppletTeam;
import com.ruoyi.applet.domain.TopData;
import com.ruoyi.applet.service.IAppletTeamService;
import com.brisk.common.annotation.Log;
import com.brisk.common.core.controller.BaseController;
import com.brisk.common.core.domain.AjaxResult;
import com.brisk.common.core.page.TableDataInfo;
import com.brisk.common.enums.BusinessType;
import com.brisk.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * appletTeamController
 *
 * @author PengFeiDeng
 * @date 2022-05-30
 */
@RestController
@RequestMapping("/applet/appletTeam")
public class AppletTeamController extends BaseController {
    @Autowired
    private IAppletTeamService appletTeamService;


    /**
     * 获取首页顶部数据
     *
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PengFei Deng
     * @date 2022/6/3 2:41
     */
    @GetMapping("getIndexTopData")
    public AjaxResult getIndexTopData() {
        TopData topData = appletTeamService.getIndexTopData();
        BigDecimal todayMileage;
        if (topData.getTodayWalkSum() == null) {
            todayMileage = new BigDecimal("0.00");
            topData.setTodayWalkSum(0L);
        } else {
            todayMileage = new BigDecimal(topData.getTodayWalkSum()).divide(new BigDecimal(20), 2, RoundingMode.FLOOR);
        }
        topData.setTodayMileageSum(todayMileage);
        return AjaxResult.success(topData);
    }


    /**
     * 获取队伍信息 柱子状图
     *
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PengFei Deng
     * @date 2022/6/3 0:37
     */
    @GetMapping("getTeamCharBar")
    public AjaxResult getTeamCharBar() {
        List<String> xData = new ArrayList<>(); // 横坐标 纵坐标
        List<BigDecimal> walkSum = new ArrayList<>();
        List<Integer> peopleSum = new ArrayList<>();
        List<Integer> daySum = new ArrayList<>();
        List<AppletTeam> appletTeamList = appletTeamService.getTeamRankingList(new AppletTeam());
        appletTeamList = appletTeamList.stream().sorted((a, b) -> b.getWalkSum().compareTo(a.getWalkSum())).collect(Collectors.toList());
        int size = Math.min(appletTeamList.size(), 10);
        for (int i = 0; i < size; i++) {
            AppletTeam appletTeam = appletTeamList.get(i);
            xData.add(appletTeam.getTeamName());
            walkSum.add(appletTeam.getWalkSum());
            peopleSum.add(appletTeam.getPeopleSum());
            daySum.add(appletTeam.getMaxDaySum());
        }
        Map<String, Object> res = new HashMap<>();
        res.put("xData", xData);
        res.put("walkSum", walkSum);
        res.put("peopleSum", peopleSum);
        res.put("daySum", daySum);
        return AjaxResult.success(res);
    }


    /**
     * 查询appletTeam列表
     */
    @PreAuthorize("@ss.hasPermi('applet:appletTeam:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppletTeam appletTeam) {
        startPage();
        List<AppletTeam> list = appletTeamService.selectAppletTeamList(appletTeam);
        return getDataTable(list);
    }

    /**
     * 导出appletTeam列表
     */
    @PreAuthorize("@ss.hasPermi('applet:appletTeam:export')")
    @Log(title = "appletTeam", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppletTeam appletTeam) {
        List<AppletTeam> list = appletTeamService.selectAppletTeamList(appletTeam);
        ExcelUtil<AppletTeam> util = new ExcelUtil<>(AppletTeam.class);
        util.exportExcel(response, list, "appletTeam数据");
    }

    /**
     * 获取appletTeam详细信息
     */
    @PreAuthorize("@ss.hasPermi('applet:appletTeam:query')")
    @GetMapping(value = "/{teamId}")
    public AjaxResult getInfo(@PathVariable("teamId") Long teamId) {
        return AjaxResult.success(appletTeamService.selectAppletTeamByTeamId(teamId));
    }

    /**
     * 新增appletTeam
     */
    @PreAuthorize("@ss.hasPermi('applet:appletTeam:add')")
    @Log(title = "appletTeam", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppletTeam appletTeam) {
        return appletTeamService.insertAppletTeam(appletTeam);
    }

    /**
     * 修改appletTeam
     */
    @PreAuthorize("@ss.hasPermi('applet:appletTeam:edit')")
    @Log(title = "appletTeam", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppletTeam appletTeam) {
        return toAjax(appletTeamService.updateAppletTeam(appletTeam));
    }

    /**
     * 删除appletTeam
     */
    @PreAuthorize("@ss.hasPermi('applet:appletTeam:remove')")
    @Log(title = "appletTeam", businessType = BusinessType.DELETE)
    @DeleteMapping("/{teamIds}")
    public AjaxResult remove(@PathVariable Long[] teamIds) {
        return toAjax(appletTeamService.deleteAppletTeamByTeamIds(teamIds));
    }


    /**
     * 获取队伍下拉选择框
     *
     * @author PengFei Deng
     * @date 2022/6/3 14:55
     */
    @GetMapping("/getTeamSelect")
    public AjaxResult getTeamSelect() {
        List<AppletSelect> teamSelectList = appletTeamService.getTeamSelectList();
        return AjaxResult.success(teamSelectList);
    }


}
